package com.gg.fmallspringboot.controller;
/* stohox
2021/6/18 11:37 */
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.gg.fmallspringboot.config.AlipayConfig;
import com.gg.fmallspringboot.entity.Paylog;
import com.gg.fmallspringboot.model.Result;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/alipay")
public class AlipayController {
//    private final String APP_ID = "2021000117674730";
//    private final String APP_PRIVATE_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsjjoipJgw52XENcPP/xajMEg78zzFAXCm5DsJ6n/E78wDU11LfYMRc/9wHZW9k/IhVLfVFJEber32jZZjRh8S2GMcGPeaoj8G+2IQLTvvvibBAsRTGUFDZv6NfeMeCeTG0cgAy3cg6Xpi9UKKLB8WRHJRBoZCsmJIE3bkZ4f0SaFsfb9R3KZYr3AxLb9BVyxgxczK2mu1YSV5KeInCX7fvGv85HiT2CEN9BwA2vuBa3fOOBnxx8TM6usZywIKRajXVj87nChgwn2EGF3I40obHdVCVUdYbXffZMJQFsh4SZziO/BAV4npCJI4uwuoGTlHtj9S06DwMKoCvRLVz7EjQIDAQAB";
//    private final String CHARSET = "UTF-8";
//    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgBALUDAw8GcFi+kN35JJSOYbC7au1x4uOdxCdBm9y9TeoJ7XMXKpMqKOFRDIdpuOvvg4G/8lSHqPijcN6aAvqC/Zfjo28wm4sN7X40RpBtkcKpVDmPCpjk9rpIO/i/nZFQ823RW9jOIBKn2+2jsvpJwxa3uFEBcsupkXmP0tOBKEwHHfk9jgiZ+8Ajw7wF9VTriUSQabyamm+V75MfZkvGgu+sagm4eGRFVR4cJ7vfa2dd7MOY09qOP8FlJiP0mq4DojF2Qgp8VWyBULZnL5jbiIkbZayqOloDd+YsoliPOsX7XX/CGSPUz1HxIzZQnlodQG3bkCQPxeK8EZ3w3ewQIDAQAB";
//    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
//    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
//    private final String FORMAT = "JSON";
//    //签名方式
//    private final String SIGN_TYPE = "RSA2";
//    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
//    private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl";
//    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
//    private final String RETURN_URL = "http://127.0.0.1/returnUrl";
    @GetMapping
    public Result toPay(Paylog paylog){
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        //用户支付行为后的跳转地址，参数为带http的完整地址
        request.setReturnUrl(AlipayConfig.return_url);
        request.setNotifyUrl(AlipayConfig.notify_url);

        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        //设置待支付物品
        request.setBizModel(model);
        //订单编号
        model.setOutTradeNo(Long.toString(System.currentTimeMillis()));
        //订单价格
        model.setTotalAmount("999");
        //订单内容
        model.setSubject("魔幻手机12 256G");
        try {
            AlipayTradePrecreateResponse response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return new Result(0,"支付申请失败",null);
    }


    /*
    * 支付宝异步通知接口
    * */
    @RequestMapping("aliapy_notify")
    @ResponseBody
    public String alipayNotify(HttpServletRequest request){
        return "success";
    }

















//    @RequestMapping("aliapy")
//    public void alipay(HttpServletResponse httpResponse) throws IOException {
//
//        SecureRandom r= new SecureRandom();
//        //实例化客户端,填入所需参数
//        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
//        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//        //在公共参数中设置回跳和通知地址
//        request.setReturnUrl(RETURN_URL);
//        request.setNotifyUrl(NOTIFY_URL);
//
//        //商户订单号，商户网站订单系统中唯一订单号，必填
//        //生成随机Id
//        String out_trade_no = UUID.randomUUID().toString();
//        //付款金额，必填
//        String total_amount =Integer.toString(r.nextInt(9999999)+1000000);
//        //订单名称，必填
//        String subject ="奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
//        //商品描述，可空
//        String body = "尊敬的会员欢迎购买奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
//        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
//                + "\"total_amount\":\""+ total_amount +"\","
//                + "\"subject\":\""+ subject +"\","
//                + "\"body\":\""+ body +"\","
//                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//        String form = "";
//        try {
//            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
//        } catch (AlipayApiException e) {
//            e.printStackTrace();
//        }
//        httpResponse.setContentType("text/html;charset=" + CHARSET);
//        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
//        httpResponse.getWriter().flush();
//        httpResponse.getWriter().close();
//    }


    }

